<template>
  <div>

    <div class="card" style="margin-bottom: 10px;background-color: bisque">
      <el-input style="width: 300px;margin-right: 10px" v-model="data.username" placeholder="请输入学号查询" :prefix-icon="Search"/>
      <el-input style="width: 300px;margin-right: 10px" v-model="data.name" placeholder="请输入名称查询" :prefix-icon="Search"/>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="info" @click="reset">重置</el-button>
    </div>

    <div class="card" style="margin-bottom: 10px; background-color: bisque">

      <div style="margin-bottom: 10px">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>

      <el-table stripe :data="data.tableData" :cell-style="cellStyle">
        <el-table-column label="序号" prop="id"></el-table-column>
        <el-table-column label="学生账号" prop="username"></el-table-column>
        <el-table-column label="学生名称" prop="name"></el-table-column>
        <el-table-column label="学生手机号" prop="phone"></el-table-column>
        <el-table-column label="学生邮箱" prop="email"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="生日" prop="birth"></el-table-column>
        <el-table-column label="头像" prop="avatar">
          <template #default="scope">
            <el-image :src = "scope.row.avatar" :preview-src-list = "[scope.row.avatar]" style="width: 40px;height: 40px"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180">
          <template #default="scope">
            <el-button type="primary" plain @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

<!--    分页-->
    <div class="card" style="background-color: bisque">
      <el-pagination v-model:current-page="data.pageNum" v-model:page-size="data.pageSize"
                     @current-change="handelCurrentChange"
                     background layout="prev, pager, next" :total="data.total" />
    </div>


    <el-dialog with="35%" v-model="data.formVisible" title="学生信息" width="40%" destroy-on-close>
      <el-form :model="data.form" :rules="rules" ref="formRef" label-with="100px" style="padding-right: 30px">
        <el-form-item label="学生账号" prop="username" :label-width="formLabelWidth">
          <el-input v-model="data.form.username" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生密码" prop="password">
          <el-input v-model="data.form.password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="学生名称" >
          <el-input v-model="data.form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="手&nbsp;机&nbsp;号" >
          <el-input v-model="data.form.phone" autocomplete="off" />
        </el-form-item>
        <el-form-item label="邮&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;箱" >
          <el-input v-model="data.form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别" >
          <el-radio-group v-model="data.form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;日" >
          <el-date-picker style="width: 100%" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="data.form.birth"></el-date-picker>
        </el-form-item>
        <el-form-item label="头&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;像" >
          <el-upload action="http://localhost:9090/files/upload" list-type="picture" :on-success="handelImgUploadSuccess">
            <el-button type="primary">上传头像</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="data.formVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">保 存</el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import request from "@/utils/request";
import {ref,reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import {ElMessage, ElMessageBox} from "element-plus";

// request.get('/').then(res => {
//   console.log(res)
// })

const data = reactive({
  username:'',
  name:'',
  pageNum: 1,
  pageSize:5,
  formVisible: false,
  form: {},
  tableData: [],
  total: 0
})

const formRef = ref()

const load = () =>{
  request.get('/student/selectPage',
      {
        params: {
          pageNum: data.pageNum,
          pageSize: data.pageSize,
          username:data.username,
          name:data.name
        }
      }).then(res=>{
        //console.log(res)
        data.tableData=res.data?.list || []
        data.total=res.data?.total || 0
  })
}
//调用后台数据
load()
const handelCurrentChange = () => {
  // 当翻页的时候重新加载数据即可
  load()
}
const reset =() => {
  data.username = ''
  data.name = ''
  load()
}
//内置参数rules，实现账号密码校验
const rules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
})
const handleAdd = () => {
  data.form = {}
  data.formVisible = true
}
//保存数据到后台
// 保存数据到后台
const save = () => {
  formRef.value.validate((valid) => {
    if(valid){
      request.request({
        url: data.form.id ? '/student/update' : '/student/add',
        method: data.form.id ? 'PUT' : 'POST',
        data: data.form
      }).then(res => {
        if (res.code === '200') {
          load()    // 重新获取数据
          data.formVisible = false  // 关闭弹窗
          ElMessage.success("操作成功")
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
  })
}
const handleEdit = (row) => {
  data.form = JSON.parse(JSON.stringify(row))
  data.formVisible = true
}
const del = (id) => {
  ElMessageBox.confirm('删除数据后无法恢复，您确认删除吗？', '删除确认', { type: 'warning' }).then(res => {
    request.delete('/student/delete/' + id).then(res => {
      if (res.code === '200') {
        load()    // 重新获取数据
        ElMessage.success("操作成功")
      } else {
        ElMessage.error(res.msg)
      }
    })
  }).catch(res => {
    ElMessage({
      type: 'info',
      message: '您已取消',
    })
  })
}

const handelImgUploadSuccess = (res) => {
    data.form.avatar = res.data;
}
const cellStyle = () => {
  return { backgroundColor: "bisque", color: "#333333" };
};
</script>